# -*- coding: utf-8 -*-
# @File    : get_sql_stam.py
# @Date    : 2021-11-25
# @Author  : Richard

import os
import re
import time



# 解析SQL文件
class ResoluSql(object):
    def __init__(self, file):
        self.filepath = file

    #
    def ergodic_sql(self):
        """
        :param:sql_file_path
        :return:  sqls info list
        """
        try:
            with open(self.filepath) as f:
                sqls = f.readlines(100000)
                return sqls
        except Exception as Err:
            print("ergodic statement error is :{0}".format(Err))

    # 解析SQL文件
    def get_sql_file(self):
        """
        功能：根据目录遍历SQL文件
        :param:sql_file_path
        :return:  SQL file ，data type is list
        """
        try:
            for root, dirs, files in os.walk(self.filepath):
                data = []
                for file in files:
                    res = os.path.join(root, file).encode("utf8")
                    data.append(res)
                return data
        except Exception as Err:
            print("ergodic statement error is :{0}".format(Err))


    def open_sql_file(self, sql_file):
        """
        功能：根据SQL文件解析出SQL语句
        :param sql_file:
        :return: sql statement list
        """
        if sql_file:
            try:
                # with open(sql_file, encoding="GBK") as f:
                with open(sql_file, encoding="utf-8") as f:
                    # res = f.readlines()
                    res = f.read()
                    if re.findall(r"begin", res) and re.findall(r"create", res):
                        resource = res.split("--")
                        codeline = 1
                    # 非代码块SQL
                    else:
                        resource = res.split(";")[:-1]
                        codeline = 0
                    return [resource, codeline]
            except Exception as Err:
                print("open sql file error,information more check[0]".format(Err))
        else:
            print("sql file not exists")

    def read(self, sql_file):
        """
        功能：根据SQL文件解析出SQL语句
        :param sql_file:
        :return: sql statement list
        """
        if sql_file:
            try:
                # with open(sql_file, encoding="GBK") as f:
                with open(sql_file, encoding="utf8") as f:
                    # res = f.readlines()
                    res = f.read()
                    if res:
                        return res
            except Exception as Err:
                print("open sql file error,information more check[{0}]".format(Err))
        else:
            print("sql file not exists")

    def read_by_row(self, file):
        """
        功能：根据SQL文件解析出SQL语句,以行的方式
        :param sql_file:sql文件
        :return: sql statement list
        """
        if file:
            try:
                # with open(file, encoding="GBK") as f:
                with open(file, encoding="utf8") as f:
                    res = f.read().split("\n")
                    if res:
                        return res
            except Exception as Err:
                print("open sql file error,information more check[0]".format(Err))
        else:
            print("sql file not exists")

